//
//  DataManager.m
//  CarService
//
//  Created by Nimo.Yu on 15/1/31.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "DataManager.h"

@implementation DataManager
// 获取使用实例
+ (instancetype )sharedInstance
{
    static DataManager *sharedInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedInstance = [[super alloc] init];
        [sharedInstance initManager];
    });
    return sharedInstance;
}

// 初始化Manager
- (void) initManager
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    //读取用户信息
    self.workId = [userDefaults objectForKey:KeyWorkId];
    self.userId = [userDefaults objectForKey:KeyUserId];
    self.usersId = [userDefaults objectForKey:KeyUsersId];
    self.userPassword = [userDefaults stringForKey:KeyUserPassWord];
    self.nickName = [userDefaults stringForKey:KeyNickName];
    self.proxy = [userDefaults stringForKey:KeyProxy];
    self.isReservation = [userDefaults stringForKey:KeyReservation];
    _testHost = [userDefaults stringForKey:KeyTestHost];
    NSNumber *isSubscribe=(NSNumber *)[userDefaults stringForKey:KeySubscribeActivity];
    if (isSubscribe) {
        _isSubscribeActivityPushMessage=[isSubscribe boolValue];
    }else{
        _isSubscribeActivityPushMessage=YES;
    }
    _userType=[userDefaults stringForKey:KeyUserType];
    _watermarkOne = [userDefaults stringForKey:KeyWatermarkOne];
    _watermarkTwo = [userDefaults stringForKey:KeyWatermarkTwo];
    _watermarkThree = [userDefaults stringForKey:KeyWatermarkThree];
}

// 清空用户
- (void) clearUserDefaults
{
    self.userPassword = nil;
    self.userId=nil;
    _watermarkOne=nil;
    _watermarkTwo=nil;
    _watermarkThree=nil;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults removeObjectForKey:KeyUserId];
        [userDefaults removeObjectForKey:KeyUserPassWord];
        [userDefaults removeObjectForKey:KeyWatermarkOne];
        [userDefaults removeObjectForKey:KeyWatermarkTwo];
        [userDefaults removeObjectForKey:KeyWatermarkThree];
        [userDefaults synchronize];
    });
}

//保存用户数据
- (void) saveUserInfo
{
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:self.userId forKey:KeyUserId];
        [userDefaults setObject:self.userPassword forKey:KeyUserPassWord];
        [userDefaults setObject:self.nickName forKey:KeyNickName];
        [userDefaults setObject:self.proxy forKey:KeyProxy];
        [userDefaults setObject:self.workId forKey:KeyWorkId];
        [userDefaults setObject:self.isReservation forKey:KeyReservation];
        [userDefaults setObject:self.servicePhone forKey:KeyServicePhone];
        [userDefaults setObject:self.usersId forKey:KeyUsersId];
        [userDefaults synchronize];
    });
}

///设置用户信息
- (void) setUserInfoWithDic:(NSDictionary *)userInfo
{
    [self saveUserInfo];
}


///是否登录
- (BOOL) isLogin
{
    if (self.userId) {
        return YES;
    }
    return NO;
}

- (void)setTestHost:(NSString *)testHost
{
    _testHost=testHost;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:_testHost forKey:KeyTestHost];
        [userDefaults synchronize];
    });
}

- (void)setIsSubscribeActivityPushMessage:(BOOL)isSubscribeActivityPushMessage{
    _isSubscribeActivityPushMessage=isSubscribeActivityPushMessage;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:@(_isSubscribeActivityPushMessage) forKey:KeySubscribeActivity];
        [userDefaults synchronize];
    });
}

#pragma mark - Tools Function

// 沙盒路径
+ (NSString *)getDocumentPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documents = [paths objectAtIndex:0];
    return documents;
}

// 获取目录
+ (NSString *)getDocumentWithName:(NSString *)name
{
    if (name == nil) {
        return nil;
    }
    
    NSString *documents = [DataManager getDocumentPath];
    NSString *database_path = [documents stringByAppendingPathComponent:name];
    NSFileManager *wk_manager = [NSFileManager defaultManager];
    BOOL wk_isExist,wk_isDirectory;
    wk_isExist = [wk_manager fileExistsAtPath:database_path isDirectory:&wk_isDirectory];
    if(wk_isExist == NO || wk_isDirectory == NO)
    {
        [wk_manager createDirectoryAtPath:database_path withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    return database_path;
}

- (void)setUserType:(NSString *)userType{
    _userType=userType;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:_userType forKey:KeyUserType];
        [userDefaults synchronize];
    });
}

- (void)setWatermarkOne:(NSString *)watermarkOne{
    _watermarkOne=watermarkOne;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:_watermarkOne forKey:KeyWatermarkOne];
        [userDefaults synchronize];
    });
}

- (void)setWatermarkThree:(NSString *)watermarkThree{
    _watermarkThree=watermarkThree;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:_watermarkThree forKey:KeyWatermarkThree];
        [userDefaults synchronize];
    });
}

- (void)setWatermarkTwo:(NSString *)watermarkTwo{
    _watermarkTwo=watermarkTwo;
    dispatch_async(dispatch_get_main_queue(), ^{
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:_watermarkTwo forKey:KeyWatermarkTwo];
        [userDefaults synchronize];
    });
}
@end
